<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon
              icon-class="nested"
              style="margin-right:10px"
            /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树形组件的使用 -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!-- #default === slot-scope
            什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)-->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <svg-icon icon-class="user" style="margin-right:10px" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- 实现新增子部门或编辑部门 -->
      <el-dialog
        :title=" isEdit ? '编辑' : '添加' "
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >
        <!-- 解决方案1：在关闭弹层时，把子组件销毁 v-if="showDialog" -->
        <dept-dialog
          v-if="showDialog"
          :id="curId"
          ref="depDialogRef"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="hClose"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { delDepartments, getDepartments } from '@/api/departments'
import { tranListToTreeList } from '@/utils'
import deptDialog from './deptDialog.vue'

export default {
  components: { deptDialog },
  data() {
    return {
      // 保存部门的数据列表
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false, // 值是false表示新增，true表示编辑
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    hClose() {
      // 重置表单
      this.$refs.depDialogRef.resetForm()
    },
    hEdit(id) {
      // 显示弹层
      this.showDialog = true
      this.isEdit = true
      // 更新当前操作的id
      this.curId = id
      // 获取子组件的引用
      /*
      解决方案2：在父组件中通过引用找到子组件

      this.$nextTick(() => {
        // 调用子组件的方法
        this.$refs.deptDialog.loadDepartmentsDetail()
      })
      */
    },
    hAdd(id) {
      // 显示弹层
      this.showDialog = true
      this.isEdit = false
      // 更新当前操作的id
      this.curId = id
    },
    async loadDepartments() {
      // 发送请求获取部门数据
      try {
        const { data: res } = await getDepartments()
        console.log('getDepartments', res)
        this.originList = res.depts.map(item => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        // shift()从数组头部取出一个数据 --- 删除数组中的第一个数据
        const company = res.depts.shift()
        console.log('删除的第一个数据', company)
        console.log('部门数据', res.depts)
        // 将平铺的数组转成树型结构数据
        this.list = tranListToTreeList(res.depts)
      } catch (error) {
        console.log(error)
      }
    },
    hDel(id) {
      // 是否删除
      this.$confirm('此操作将永久删除该部门，是否继续', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 确认删除
        try {
          const res = await delDepartments(id)
          console.log('删除成功', res)
          this.$message.success(res.message)
          this.loadDepartments()
        } catch (error) {
          console.log(error)
        }
      })
    },
    hSuccess() {
      // 关闭对话框
      this.showDialog = false
      // 刷新页面
      this.loadDepartments()
    }
  }
}
</script>
